package com.hustzp.com.xichuangzhu.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

/**
 * Created by Administrator on 2016/6/5.
 */
public class AddRecmdActivity extends MyBaseActivity {

    private EditText recmdTitle;
    private EditText recmdAuthor;
    private Button recmdBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_recmd);

        initToolbar();
        initViews();
    }

    private void initToolbar() {
        TextView navTxt = (TextView) findViewById(R.id.tv_nav_txt);
        navTxt.setText("推荐收录");

        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText("推荐");
    }

    private void initViews() {
        recmdTitle = (EditText) findViewById(R.id.tv_recmd_title);
        recmdAuthor = (EditText) findViewById(R.id.tv_recmd_author);

        // 推荐
        recmdBtn = (Button) findViewById(R.id.recmd_btn);
        recmdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(recmdTitle.getText().toString().trim())) {
                    DialogFactory.hintDialog(AddRecmdActivity.this, "作品标题不可为空");
                    return;
                }

                if (TextUtils.isEmpty(recmdAuthor.getText().toString().trim())) {
                    DialogFactory.hintDialog(AddRecmdActivity.this, "作者不可为空");
                    return;
                }

                AVObject recmdObj = new AVObject("RecommendWork");
                recmdObj.put("title", recmdTitle.getText().toString().trim());
                recmdObj.put("author", recmdAuthor.getText().toString().trim());
                recmdObj.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            setResult(RESULT_OK);
                            finish();
                        } else {
                            DialogFactory.hintDialog(AddRecmdActivity.this, "推荐保存失败，请重试");
                        }
                    }
                });
            }
        });
    }

    public void recommend(View view) {
        showToastInfo("推荐");
    }

}
